package com.quick.develop.flowable.framework.cmd;

import com.quick.develop.flowable.utils.BpmTaskUtil;
import org.flowable.common.engine.impl.interceptor.Command;
import org.flowable.common.engine.impl.interceptor.CommandContext;
import org.flowable.engine.impl.util.CommandContextUtil;
import org.flowable.task.service.HistoricTaskService;
import org.flowable.task.service.impl.persistence.entity.HistoricTaskInstanceEntity;

import java.io.Serializable;
import java.util.Objects;

/**
 * desc:
 *
 * @author junyuan.liu
 * @date 2023/7/18 11:09
 */
public class SetHistoricTaskDescCmd implements Command<Void>, Serializable {

    private static final long serialVersionUID = 8464413118127769922L;
    
    protected String desc;
    protected String taskId;
    
    public SetHistoricTaskDescCmd(String taskId, String desc) {
        this.taskId = taskId;
        this.desc = desc;
    }

    @Override
    public Void execute(CommandContext commandContext) {
        HistoricTaskService historicTaskService = CommandContextUtil.getHistoricTaskService(commandContext);
        HistoricTaskInstanceEntity historicTask = historicTaskService.getHistoricTask(taskId);
        String newDesc = BpmTaskUtil.concatDescription(historicTask.getDescription(), desc);
        if (!Objects.equals(newDesc, historicTask.getDescription())) {
            historicTask.setDescription(newDesc);
            historicTaskService.updateHistoricTask(historicTask, false);
        }
        return null;
    }
}
